package com.lanou3g.demo.role.controller;

import com.lanou3g.demo.entity.ExRoleinfo;
import com.lanou3g.demo.entity.RoleInfo;
import com.lanou3g.demo.entity.RoleModule;
import com.lanou3g.demo.role.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private IRoleService iRoleService;
    @RequestMapping("/role_list_all")
    public String findAll(int start, Model model, HttpServletRequest request){
        model.addAttribute("rolelist",iRoleService.findAll((start - 1) * 7));
        model.addAttribute("totalCount",iRoleService.findTotal().getTotal());
        model.addAttribute("currentPage",request.getParameter("start"));
        return "/role/role_list";
    }
    @RequestMapping("/role_delete")
    public String deleteRole(int roleId,Model model) {
        iRoleService.deleteOne(roleId);
        iRoleService.deleteMiddle(roleId);
        return "redirect:/role/role_list_all?start=1";
    }
    @RequestMapping("/role_addOne")
    public String addOne(int totalPage,String name, @RequestParam(value = "moduleId") List<Integer> moduleId){
        iRoleService.insertRoleInfo(name);
        Integer roleId = iRoleService.selectNameById(name).getRoleId();
        iRoleService.insertRoleModule(roleId, moduleId);
        return "redirect:/role/role_list_all?start=" + totalPage;
    }
    @RequestMapping("/role_updateOne")
    public String updateOne(int roleId, int start, String name, @RequestParam(value = "moduleId") List<Integer> moduleId){
        //根据roleID修改name
        iRoleService.updateName(roleId,name);
        //通过roleId查询ModuleId二进制和
        RoleModule allModule = iRoleService.findAllModule(roleId);
        int current_module = allModule.getModuleId();
        //整合前端数据,获得更新后的ModuleId二进制和
        int new_module = 0;
        for (Integer integer : moduleId) {
            new_module += 1<<(integer-1);
        }
        //计算出需更新、删除的ModuleId二进制和
        int public_module = new_module & current_module;
        int insert = new_module - public_module;
        int delete = current_module - public_module;
        //有更新项执行插入
        if (insert > 0){
            List<Integer> insertModule = parsing(insert);
            // 执行插入
            iRoleService.insertModule(roleId,insertModule);
        }
        //有删除项执行删除
        if (delete > 0){
            List<Integer> deleteModule = parsing(delete);
            //执行删除
            iRoleService.deleteModule(roleId,deleteModule);
        }
        return "redirect:/role/role_list_all?start=" + start;
    }
    //解析二进制和,生成ID集合
    public static List<Integer> parsing(int param){
        List<Integer> result = new ArrayList<>();
        for (int i = 7,index = 64; i > 0; i--) {
            if (param >= index){
                result.add(i);
                param -= index;
            }
            index >>= 1;
        }
        return result;
    }
}
